package com.durablemethods.tsmserver;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.durablemethods.tsmserver.home.TeamsHome;
import com.durablemethods.tsmserver.home.TraderPositionSummaryHome;
import com.durablemethods.tsmserver.model.TraderPositionSummary;

public class PayoutMatrixServlet extends HttpServlet {
	private TeamsHome teamsHome = new TeamsHome();
	private TraderPositionSummaryHome tpsHome = new TraderPositionSummaryHome();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/plain");
		List<String> teamNames = teamsHome.getTeamNames();
		StringBuilder header = new StringBuilder();
		header.append("Trader");
		for (String string : teamNames) {
			header.append(",").append(string);
		}
		String headerString = header.toString();
		resp.getWriter().println(headerString);
		List<TraderPositionSummary> positionSummaries = tpsHome.getPositionSummaries();
		for (TraderPositionSummary traderPositionSummary : positionSummaries) {
			StringBuilder row = new StringBuilder();
			row.append(traderPositionSummary.getTrader());
			int size = teamNames.size();
			for (int i = 0; i < size; i++) {
				String team = teamNames.get(i);
				Double double1 = traderPositionSummary.getSharesByTeamMap().get(team);
				if (double1 == null){
					double1=0.0;
				}
				row.append(",").append(new BigDecimal(double1).setScale(2, RoundingMode.HALF_DOWN));
			}
			resp.getWriter().println(row.toString());
		}
	}

}
